package com.bjdd.jdy.ui.pay;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.OrderBean;
import com.bjdd.jdy.entity.PayBean;
import com.bjdd.jdy.util.ImageUtil;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.bjdd.jdy.view.other.CustomDialog;
import com.google.zxing.WriterException;
import com.google.zxing.encoding.EncodingHandler;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayActivity extends BaseActivity<PayPresenter, PayModel> implements PayContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.pay_qrcode)
    ImageView payQrcode;
    @Bind(R.id.btn_pay_other)
    Button btnPayOther;
    @Bind(R.id.btn_pay_finish)
    Button btnPayFinish;
    private ImageUtil imageUtil;
    private String orderNo;
    private String APP_ID = "wx000b05d8ed76b3a5";
    private IWXAPI api;
    @Override
    public int getLayoutId() {
        return R.layout.activity_pay;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.toolbar_bg), 0);
        title.setText("支付费用");
        imageUtil = new ImageUtil(context);
        Bundle bundle = getIntent().getExtras();
        orderNo = bundle.getString("orderNo");
        double lat = bundle.getDouble("lat");
        double lng = bundle.getDouble("lng");
        presenter.isCanSign(lat,lng);
        api = WXAPIFactory.createWXAPI(context,null);
        api.registerApp(APP_ID);
    }

    @Override
    public void codSuccess(String url) {
        if (url == null)
            return;
        try {
            Bitmap mBitmap = EncodingHandler.createQRCode(url, 300);
            payQrcode.setImageBitmap(mBitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        //imageUtil.loadUrlImage(url,payQrcode);

    }

    @Override
    public void paySuccess(PayBean payBean) {
        PayReq request = new PayReq();
        L.d("报名："+payBean.getPackageX());
        request.appId = payBean.getAppid();
        request.partnerId = payBean.getPartnerid();
        request.prepayId = payBean.getPrepayid();
        request.packageValue = payBean.getPackageX();
        request.nonceStr = payBean.getNoncestr();
        request.timeStamp = payBean.getTimestamp();
        request.sign = payBean.getSign();
        api.sendReq(request);
    }

    @Override
    public void canNotSign() {
        CustomDialog.Builder builder;
        builder = new CustomDialog.Builder(this);
        mDialog = builder.setMessage("当前不在签收范围")
                .setSingleButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                })
                .createSingleButtonDialog();
        mDialog.show();
    }

    @Override
    public void canSign() {
        presenter.getUrl(orderNo);
    }

    @Override
    public void payBack() {
        JumpUtil.toOrder(context);
        finish();
    }


    @OnClick({R.id.toolbar_back, R.id.btn_pay_other, R.id.btn_pay_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.btn_pay_other:
                presenter.helpPay(orderNo);
                break;
            case R.id.btn_pay_finish:
                break;
        }
    }
}
